package demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestHTTP {
	private static String sessionid = null;

	private static String cookieval = null;
	
	public static String APPID = "wx32ecb71139189171";

	public static String SECRET = "94eb0f21f1bb2947fded40d09c4169b9";

	public static void main(String[] args) {
		
		try {
			String code = "0013udNi2pvEII0ZapOi2KX9Ni23udNN";
			URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?" +
				"appid=" + APPID +
				"&secret=" + SECRET +
				"&code=" + code +
				"&grant_type=authorization_code");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			if (cookieval != null) {
				sessionid = cookieval.substring(0, cookieval.indexOf(";"));
				//System.out.println(sessionid);
			}
			
			conn.setRequestMethod("GET");
			// conn.setRequestProperty("cookie",
			// "JSESSIONID=1C86B8B34A276EC271EEFC11FDD88827");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(5000);
			conn.setUseCaches(false);
			conn.connect();

			// String cookieval = conn.getHeaderField("set-cookie");
			// System.out.println(cookieval);

			BufferedReader br = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));
			StringBuffer buffer = new StringBuffer();
			String str = "";
			while ((str = br.readLine()) != null) {
				buffer.append(str);
			}
			br.close();
			String jsonStr = buffer.toString();
			System.out.println(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
